package com.ha.springcloud.controller;


import com.ha.springcloud.pojo.Department;
import com.ha.springcloud.pojo.Employee;
import com.ha.springcloud.service.DepartmentService;
import com.ha.springcloud.service.EmployeeService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//实现页面跳转不能使用restcontroller
@RestController
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private DepartmentService departmentService;

//    @RequestMapping("/queryEmployeeList")
//    public String  queryemployeeList(){
//        return "hello";
//    }
@RequestMapping("/queryEmployeeList")
public List<Employee> queryemployeeList(){
    System.out.println("provider");
    List<Employee> employeeList =employeeService.queryEmployeeList();
    System.out.println(employeeList);
    return employeeList;
}

    @HystrixCommand(fallbackMethod = "HystrixGet")//服务访问失败所调用的方法
    @RequestMapping("/emp/get/{id}")
    public Employee get(@PathVariable("id") String id){
        Employee employee = employeeService.queryEmployeeById(id);

        if(employee==null){
            throw new RuntimeException("id=>"+id+"不存在，或者信息无法访问");
        }
        return employee;
    }

    public Employee HystrixGet(@PathVariable("id") String id){
        return new Employee().setId(id).
                setName("id=>"+id+"不存在，或者信息无法访问--Hystrix");
    }


    @RequestMapping("/maxId")
    public String getMaxId(){
        int maxId = employeeService.getMaxId();
        String id=""+maxId;
        return id;
    }

    @RequestMapping("/getDep")
    public List<Department> getDep(){
        List<Department> departments = departmentService.queryDepartmentList();
        System.out.println(departments);
        return departments;
    }

    @PostMapping("/addEmp")
    public int addEmployee(@RequestBody Employee employee){

        System.out.println("8001"+employee);
        return employeeService.addEmployee(employee);
    }

    @PostMapping("/updateEmp")
    public int updateEmp(@RequestBody Employee employee){
        return employeeService.updateEmployee(employee);
    }

    @GetMapping("/delete/{id}")
    public int deleteEmp(@PathVariable String id){
        return employeeService.deleteEmployee(id);
    }

}
